<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* http response building service
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 
*/
class wakResponseBuilder extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access public
	*/
	function service_version() {
		$this->__construct();
	}/**/
	
	/**
	* "real" constructor
	* @access public
	*/
	function __construct() {
		parent::__construct();
		// getting service references
		$this->debug=&wak::getservice('wakdebug');
		$this->lib=&wak::getservice('waklib');
		// getting libraries
		$this->lib->needClass('httpresponsebean');
		$this->lib->needclass('datapath');
		// building response instance
		$this->bean=&new httpResponseBean();
		//echo $this->debug->vardump($this);
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* add some content to http response
	* @param string $content
	* @param string $path optional
	* @access public
	* @return bool
	*/
	function addContent($content, $path='') {
		// getting previous content
		if ($oldContent=datapath::get($this->contents,$path))
			// concatenating new content to old one
			$content=$oldContent.$content;
		// setting new value
		return datapath::set($this->contents,$content,$path);
		/*if (!is_string($index)) {
			// no valid index provided, just appending content
			$this->contents[]=&$content;
		} else {
			$this->contents[$index][]=&$content;
		}
		return true;*/
	}/**/
	
	/**
	* sets content
	* either whole content, either content at provided index
	* overwrite old content
	* @param string $content
	* @access public
	* @return bool
	*/
	function setContent($content,$path='') {
		return datapath::set($this->contents,$content,$path);
		//echo wakdebug::vardump($this->contents);
		//return true;
	}/**/
	
	/**
	*
	*/
	function setHeader($key,$value) {
		return $this->bean->setHeader($key,$value);
	}
	
	/**
	* sends response headers to browser
	* @param
	* @access public
	* @return 
	*/
	function sendHeaders() {
		return $this->bean->sendHeaders();
	}/**/
	
	/**
	* sends http response content to browser
	* @param
	* @access public
	* @return bool
	*/
	function sendContent() {
		// building content string
		$content=$this->_buildContentString();
		$this->bean->setContent($content);
		return $this->bean->sendContent();
	}/**/
	
	/**
	* sets template id to be used
	* @param string $id
	* @access public
	* @return bool
	*/
	function setTemplateId($id) {
		$this->template=$id;
		return true;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return bool
	*/
	function setCookie($key,$value=null,$expire=null) {
		$path='/'.BASE_PATH;
		//echo "$key=$value (".($expire-time()).", $path)";
		return setCookie($key,$value,$expire,$path);
	}/**/
	
	/**
	* th
	* @param
	* @access public
	* @return bool
	*/
	function throwErrorPage($code=404) {
		header("HTTP/1.0 $code");
		// setting special template
		if (isset($this->cfg['error_pages'][$code]))
			$this->setTemplateId($this->cfg['error_pages'][$code]);
		// producing content
		$this->sendContent();
		//echo $code;
		exit;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var httpResponseBean
	*/
	var $bean;
	/**
	* @var array response contents
	*/
	var $contents=array();
	/**
	* @var array config
	*/
	var $cfg=array(
		'templating'=>false
		, 'default_template'=>'httpresponse-default'
	);
	/**
	* @var string template to use (template id)
	*/
	var $template;
	/**
	*
	*/
	var $cookies=array();
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* builds response content as a string
	* @param
	* @access private
	* @return string
	*/
	function _buildContentString() {
		$str='';
		//echo wakdebug::vardump($this->contents);
		if (!$this->cfg['templating']) {
			// templating is not used
			foreach ($this->contents as $part) {
				if (is_array($part))
					$part=join(' ',$part);
				$str.=$part;
			}
		} else {
			// building from template
			// getting template id for page if needed
			
			// getting template instance
			if (!$tpl=$this->_getTemplate($this->template)) {
				trigger_error("wakResponseBuilder: failed to get page template ".$this->template);
				return false;
			}
			// injecting data
			$tpl->setData($this->contents);
			// rendering
			$str=$tpl->render();
		}
		return $str;
	}/**/
	
	/**
	* retrieve template instance by id
	* @param string $template_id
	* @access private
	* @return wakTemplate
	*/
	function & _getTemplate($template_id) {
		if (!$template_id)
			$template_id=$this->cfg['default_template'];
		$storage=&wak::getservice('waktemplatestorage');
		$tpl=$storage->getbyid($template_id);
		return $tpl;
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
